<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="激活码" prop="activationCode">
        <el-input
          v-model="queryParams.activationCode"
          placeholder="请输入激活码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="专属权益名称" prop="exclusiveRightsName">
        <el-input
          v-model="queryParams.exclusiveRightsName"
          placeholder="请输入专属权益名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="就诊人姓名" prop="patientName">
        <el-input
          v-model="queryParams.patientName"
          placeholder="请输入就诊人姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="证件类型" prop="idType">
        <el-select v-model="queryParams.idType" placeholder="请选择证件类型" clearable>
          <el-option
            v-for="dict in dict.type.document_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="证件号码" prop="idNumber">
        <el-input
          v-model="queryParams.idNumber"
          placeholder="请输入证件号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系电话" prop="contactNumber">
        <el-input
          v-model="queryParams.contactNumber"
          placeholder="请输入联系电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="备用电话" prop="backupNumber">
        <el-input
          v-model="queryParams.backupNumber"
          placeholder="请输入备用电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="医保卡号" prop="insuranceCardNumber">
        <el-input
          v-model="queryParams.insuranceCardNumber"
          placeholder="请输入医保卡号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="异地就诊" prop="outOfTown">
        <el-select v-model="queryParams.outOfTown" placeholder="请选择异地就诊" clearable>
          <el-option
            v-for="dict in dict.type.sys_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="医院城市" prop="hospitalCity">
        <el-input
          v-model="queryParams.hospitalCity"
          placeholder="请输入医院城市"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="医院名称" prop="hospitalName">
        <el-input
          v-model="queryParams.hospitalName"
          placeholder="请输入医院名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="科室名称" prop="departmentName">
        <el-input
          v-model="queryParams.departmentName"
          placeholder="请输入科室名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预期时间" prop="expectedTime">
        <el-date-picker clearable
          v-model="queryParams.expectedTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择预期时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:Appointment:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:Appointment:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:Appointment:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:Appointment:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="AppointmentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="激活码" align="center" prop="activationCode" />
      <el-table-column label="专属权益名称" align="center" prop="exclusiveRightsName" />
      <el-table-column label="专属权益 ID" align="center" prop="exclusiveRightsId" />
      <el-table-column label="就诊人姓名" align="center" prop="patientName" />
      <el-table-column label="证件类型" align="center" prop="idType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.document_type" :value="scope.row.idType"/>
        </template>
      </el-table-column>
      <el-table-column label="证件号码" align="center" prop="idNumber" />
      <el-table-column label="联系电话" align="center" prop="contactNumber" />
      <el-table-column label="备用电话" align="center" prop="backupNumber" />
      <el-table-column label="医保卡号" align="center" prop="insuranceCardNumber" />
      <el-table-column label="异地就诊" align="center" prop="outOfTown">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.outOfTown"/>
        </template>
      </el-table-column>
      <el-table-column label="医院城市" align="center" prop="hospitalCity" />
      <el-table-column label="医院名称" align="center" prop="hospitalName" />
      <el-table-column label="科室名称" align="center" prop="departmentName" />
      <el-table-column label="预期时间" align="center" prop="expectedTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.expectedTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="病历资料" align="center" prop="medicalRecords" />
      <el-table-column label="住院单" align="center" prop="hospitalizationOrder" />
      <el-table-column label="备注信息" align="center" prop="notes" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:Appointment:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:Appointment:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改预约订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="激活码" prop="activationCode">
          <el-input v-model="form.activationCode" placeholder="请输入激活码" />
        </el-form-item>
        <el-form-item label="专属权益名称" prop="exclusiveRightsName">
          <el-input v-model="form.exclusiveRightsName" placeholder="请输入专属权益名称" />
        </el-form-item>
        <el-form-item label="专属权益 ID" prop="exclusiveRightsId">
          <el-input v-model="form.exclusiveRightsId" placeholder="请输入专属权益 ID" />
        </el-form-item>
        <el-form-item label="就诊人姓名" prop="patientName">
          <el-input v-model="form.patientName" placeholder="请输入就诊人姓名" />
        </el-form-item>
        <el-form-item label="证件类型" prop="idType">
          <el-select v-model="form.idType" placeholder="请选择证件类型">
            <el-option
              v-for="dict in dict.type.document_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="证件号码" prop="idNumber">
          <el-input v-model="form.idNumber" placeholder="请输入证件号码" />
        </el-form-item>
        <el-form-item label="联系电话" prop="contactNumber">
          <el-input v-model="form.contactNumber" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="备用电话" prop="backupNumber">
          <el-input v-model="form.backupNumber" placeholder="请输入备用电话" />
        </el-form-item>
        <el-form-item label="医保卡号" prop="insuranceCardNumber">
          <el-input v-model="form.insuranceCardNumber" placeholder="请输入医保卡号" />
        </el-form-item>
        <el-form-item label="异地就诊" prop="outOfTown">
          <el-select v-model="form.outOfTown" placeholder="请选择异地就诊">
            <el-option
              v-for="dict in dict.type.sys_yes_no"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="医院城市" prop="hospitalCity">
          <el-input v-model="form.hospitalCity" placeholder="请输入医院城市" />
        </el-form-item>
        <el-form-item label="医院名称" prop="hospitalName">
          <el-input v-model="form.hospitalName" placeholder="请输入医院名称" />
        </el-form-item>
        <el-form-item label="科室名称" prop="departmentName">
          <el-input v-model="form.departmentName" placeholder="请输入科室名称" />
        </el-form-item>
        <el-form-item label="预期时间" prop="expectedTime">
          <el-date-picker clearable
            v-model="form.expectedTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择预期时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="病历资料" prop="medicalRecords">
          <el-input v-model="form.medicalRecords" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="住院单" prop="hospitalizationOrder">
          <el-input v-model="form.hospitalizationOrder" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="备注信息" prop="notes">
          <el-input v-model="form.notes" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAppointment, getAppointment, delAppointment, addAppointment, updateAppointment } from "@/api/system/Appointment";

export default {
  name: "Appointment",
  dicts: ['sys_yes_no', 'document_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 预约订单表格数据
      AppointmentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        activationCode: null,
        exclusiveRightsName: null,
        patientName: null,
        idType: null,
        idNumber: null,
        contactNumber: null,
        backupNumber: null,
        insuranceCardNumber: null,
        outOfTown: null,
        hospitalCity: null,
        hospitalName: null,
        departmentName: null,
        expectedTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        activationCode: [
          { required: true, message: "激活码不能为空", trigger: "blur" }
        ],
        exclusiveRightsName: [
          { required: true, message: "专属权益名称不能为空", trigger: "blur" }
        ],
        exclusiveRightsId: [
          { required: true, message: "专属权益 ID不能为空", trigger: "blur" }
        ],
        patientName: [
          { required: true, message: "就诊人姓名不能为空", trigger: "blur" }
        ],
        idType: [
          { required: true, message: "证件类型不能为空", trigger: "change" }
        ],
        idNumber: [
          { required: true, message: "证件号码不能为空", trigger: "blur" }
        ],
        contactNumber: [
          { required: true, message: "联系电话不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询预约订单列表 */
    getList() {
      this.loading = true;
      listAppointment(this.queryParams).then(response => {
        this.AppointmentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        activationCode: null,
        exclusiveRightsName: null,
        exclusiveRightsId: null,
        patientName: null,
        idType: null,
        idNumber: null,
        contactNumber: null,
        backupNumber: null,
        insuranceCardNumber: null,
        outOfTown: null,
        hospitalCity: null,
        hospitalName: null,
        departmentName: null,
        expectedTime: null,
        medicalRecords: null,
        hospitalizationOrder: null,
        notes: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加预约订单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getAppointment(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改预约订单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAppointment(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAppointment(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除预约订单编号为"' + ids + '"的数据项？').then(function() {
        return delAppointment(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/Appointment/export', {
        ...this.queryParams
      }, `Appointment_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
